// $codepro.audit.disable lossOfPrecisionInCast, numericLiterals

// lossOfPrecisionInCast - decimals are not necessary in the code and are therefore
// truncated

// same as others with numeric literals

/**
 * Libraries used for this class
 */
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * This class handles the playing selling goods.
 * 
 * @author Jared Storey
 * @version 1.0
 *
 */
public class SpaceTraderSell extends JFrame{
	
	/**
	 * Field model
	 */
	private final Model model;
	
	/**
	 * Field view
	 */
	private final View view;
	
	/**
	 * Field button
	 */
	private final JButton button;
	
	/**
	 * Field frame
	 */
	private final JFrame frame;
	
	/**
	 * Fields index and planet
	 */
	private final int index, planet;
	
	/**
	 * Field spinner
	 */
	private final JSpinner spinner;
	
	/**
	 * Fields lblYouCanSell and lblPriceEach
	 */
	private final JLabel lblYouCanSell, lblPriceEach;
	
	/**
	 * This constructor sets up the class
	 * 
	 * @param model The model running the system
	 * @param button The button for selling
	 * @param view The display of the game
	 * @param index the index of the good
	 * @param planet The index of the planet
	 */
	public SpaceTraderSell(Model model, JButton button,
			View view, int index, int planet) {
		setSize(new Dimension(450, 185));
		setTitle("Sell Goods");
		getContentPane().setLayout(null);
		this.planet=planet;
		this.model=model;
		this.button=button;
		this.view=view;
		this.index=index;
		frame = this;
		
		spinner = new JSpinner();
		spinner.setModel(new SpinnerNumberModel(1, 1,
				model.getShip().getGoodsArray()[index].getSold(), 1));
		spinner.setBounds(272, 56, 56, 20);
		spinner.addChangeListener(listener);
		getContentPane().add(spinner);
		
		final JLabel lblHowManyWould = new JLabel("How many would you like to sell?");
		lblHowManyWould.setBounds(42, 59, 220, 14);
		getContentPane().add(lblHowManyWould);
		
		final JButton btnSell = new JButton("Sell");
		btnSell.setBounds(173, 110, 89, 23);
		btnSell.addActionListener(sellActListener);
		getContentPane().add(btnSell);
		
		lblYouCanSell = new JLabel("You can sell up to " +
				model.getShip().getGoodsArray()[index].getSold());
		lblYouCanSell.setBounds(42, 11, 143, 14);
		getContentPane().add(lblYouCanSell);
		
		final MarketPlace market = model.getUniverse().getPlanet(planet).getMarket();
		lblPriceEach = new JLabel("" + (int) ((Integer) spinner.getValue()
				* .95 * market.getGoodsArray()[index].getPrice()));
		lblPriceEach.setBounds(282, 11, 82, 14);
		getContentPane().add(lblPriceEach);
		
		lblTotal = new JLabel("Total:");
		lblTotal.setBounds(249, 11, 34, 14);
		getContentPane().add(lblTotal);
	}

	/**
	 * Checks to see if the player is selling anything
	 */
	private final ActionListener sellActListener = new ActionListener() {
		
		/**
		 * What happens when the player sells something
		 * 
		 * @param arg0 The action event being performed
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
				
			final MarketPlace market = model.getUniverse().getPlanet(planet).getMarket();
			model.getPlayer().setCredit((int) (model.getPlayer().getCredits() +
					((Integer) spinner.getValue() * .95 * market.getGoodsArray()[index]
									.getPrice())));
			model.getShip().setCargo(model.getShip().getCargo() -
					(Integer) spinner.getValue());
			model.getShip().getGoodsArray()[index].setSold(-(Integer) spinner
					.getValue());
			view.sell[index].setText(model.getShip()
					.getGoodsArray()[index].getSoldString());
			view.setBay();
			view.setCurrency();
			processWindowEvent( new WindowEvent( frame, WindowEvent.WINDOW_CLOSING) );
		}

	};
	
	/**
	 * Updates after a sale 
	 */
	private final ChangeListener listener = new ChangeListener() {
		
		/**
		 * What happens after the player sells something
		 * 
		 * @param arg0 The event that is changing
		 */
		@Override
		public void stateChanged(ChangeEvent arg0) {
			
			final MarketPlace market = model.getUniverse().getPlanet(planet).getMarket();
			lblPriceEach.setText("" + (int) ((Integer) spinner.getValue() * .95 *
					market.getGoodsArray()[index].getPrice()));
		}
	};
	
	/**
	 * Field lblTotal
	 */
	private final JLabel lblTotal;
}
